package com.th.supcom.pds.config.client.widget;

import java.util.ArrayList;
import java.util.List;

import net.carefx.fc.component.editor.client.EMREditorPanel;
import net.carefx.fc.component.editor.client.command.Command.CommandBuilder;
import net.carefx.fc.component.editor.client.data.ArgModel;
import net.carefx.fc.component.editor.client.data.InitListener;
import net.carefx.fc.component.editor.client.utils.ActivexUtils;

import com.extjs.gxt.ui.client.Style.HideMode;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONValue;
import com.th.supcom.pds.common.enums.ComponentType;
import com.th.supcom.pds.common.enums.TJToolCommand;
import com.th.supcom.pds.internal.client.model.PrinterModel;

/**
 * 获取计算机所有打印机名称下拉框
 * 
 * @author zli
 */
public class PrinterCombox extends ComboBox <PrinterModel>
{
    /** 公司版权信息. */
    public static final String copyright = "Copyright 2014, XuNuo Corporation";

    /** 提交路径. */
    public static final String SCC_BRANCH = "$URL$";

    /** 提交日期. */
    public static final String SCC_LAST_MODIFICATION_DATE = "$Date$";

    /** 最终修改人员. */
    public static final String SCC_LAST_MODIFIER_NAME = "$Author$";

    /** 最终版本号. */
    public static final String SCC_REVISION = "$Revision$";

    private EMREditorPanel m_noipEditorPanel;
    private  List <PrinterModel> m_printerList;
    /**
     * 构造函数
     * 
     * @param panel
     */
    public PrinterCombox (final ContentPanel panel)
    {
        initUI ();
        fillData (panel);
    }

    public void fillData (final ContentPanel panel)
    {
        m_noipEditorPanel = new EMREditorPanel ("pds_printer_combox" + ActivexUtils.getEMREditorIndex (),
                                                ComponentType.TJTool);
        m_noipEditorPanel.setHideMode (HideMode.OFFSETS);
        m_noipEditorPanel.setVisible (false);
        panel.add (m_noipEditorPanel);
        m_noipEditorPanel.setListener (new InitListener ()
        {
            @Override
            public void afterInit ()
            {
                final ArgModel <Object> argModel = new ArgModel <Object> ();
                final String arg = m_noipEditorPanel.syncExecute (new CommandBuilder (TJToolCommand.GET_PRINTER_NAME).buildArgModel (argModel)
                                                                                                                     .create ());
                JSONValue jSONValue = JSONParser.parseLenient (arg);
                JSONObject object = jSONValue.isObject ();
                final String args = object.get ("args").isString ().stringValue ();
                jSONValue = JSONParser.parseLenient (args);
                object = jSONValue.isObject ();
                parseArgs (object);
            }

        });
    }

    private void initUI ()
    {
        setTriggerAction (TriggerAction.ALL);
        setDisplayField ("printer");
        setWidth (30);
        store = new ListStore <PrinterModel> ();
        m_printerList = new ArrayList <PrinterModel> ();
       
       
    }

    ;

    private void parseArgs (JSONObject object)
    {
        int i = 1;
        while (object.get ("Printer" + i) != null)
        {
            PrinterModel model = new PrinterModel ();
            model.setPrinter (object.get ("Printer" + i).isString ().stringValue ());
            m_printerList.add (model);
            i++;
        }
        final String defaultPrinter = object.get ("DefaultPrinter").isString ().stringValue ();
        store.removeAll ();
        store.add (m_printerList);
    }
}
